import QtQuick 2.0
import QtQuick.Layouts 1.1
import Qt.database.sqlite 1.0

import "../mainmenu"

Item {

    property var nameList: new Array
    property var noteList: new Array
    property int itemCount: 0
    SqliteDB{
        id: db
    }

    MainmenuItem{
        id: label
        imagepath: "../../images/control.png"
        content: qsTr("控制面板")
        height: 30;
        anchors{
            top: parent.top; left: parent.left; right: parent.right
        }
    }

    ListView{
        id: listview;
        delegate: ctrldelegate; header: ctrlheader; model: ctrlmodel

        function addOn(i)
        {
            model.append({"name": nameList[i], "note": noteList[i]})
        }

        highlight: Rectangle{
//            width: listview.width; height: 30
            color: "green"
//             y: listview.currentItem.y
//            Behavior on y {
//                          SpringAnimation {
//                              spring: 5
//                              damping: 0
//                          }
//                      }
        }

//        highlightFollowsCurrentItem: false

        anchors{
            top: label.bottom; left: label.left; right: label.right; bottom: parent.bottom
            topMargin: 2; bottomMargin: 2
        }
    }

//=======listmodel & listdelegate & listheader=======
    ListModel{
        id: ctrlmodel
    }

    Component{
        id: ctrldelegate
        Item{
            id: wrapper
            width: parent.width; height: 30
            MouseArea{
                anchors.fill: parent;
                onPressed: wrapper.ListView.view.currentIndex = index
            }

            RowLayout{
                anchors.left: parent.left; anchors.verticalCenter: parent.verticalCenter; spacing: 8
                Text {
                    text: name
                    color: wrapper.ListView.isCurrentItem ? "white" : "black"
                    font.pixelSize: 25
                    Layout.preferredWidth: listview.width/3
                }

                Text {
                    text: note
                    color: wrapper.ListView.isCurrentItem ? "white" : "black"
                    font.pixelSize: 25
                    Layout.preferredWidth: listview.width*2/3
                }
            }
        }
    }

    Component{
        id: ctrlheader
        Rectangle{
            color: "lightgray"
            width: parent.width; height: 30
            RowLayout{
                anchors.left: parent.left; anchors.verticalCenter: parent.verticalCenter; spacing: 8
                Text {
                    text: qsTr("名称")
                    font.bold: true
                    font.pixelSize: 15
                    Layout.preferredWidth: listview.width/3
                }
                Text {
                    text: qsTr("备注")
                    font.bold: true
                    font.pixelSize: 15
                    Layout.preferredWidth: listview.width*2/3
                }
            }
        }
    }
//=====================================================================
    Component.onCompleted: {
        db.connectDB();
        db.getDBData(2, "select Name, Note from CtrlPanel")
        db.closeDB();

        itemCount = db.strCount()/2;
        for(var i=0; i< db.strCount(); i+=2)
        {
            nameList.push(db.getData(i));
            noteList.push(db.getData(i+1))

            listview.addOn(i/2)
        }

        console.log("控制面板中名称列表的个数为："+nameList.length)
        console.log("控制面板中备注列表的个数为："+noteList.length)

    }


}
